//
//  LYNetProvider.swift
//  TodayNews
//
//  Created by ZhangBo on 2019/6/17.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

final class LYNetProvider<Target: LYNetTargetType> {

    private var requestsArr = [DataRequest]()

    func cancelAllRequest() {
        requestsArr.forEach {
            $0.cancel()
        }
    }
}

typealias FailureHandler = (() -> Void)?

extension LYNetProvider {
    // MARK: === net request
    /// 网络请求
    ///
    /// - Parameter target: 请求的target
    /// - Parameter param: 请求的参数
    /// - Parameter success: 请求成功回调
    /// - Parameter failure: 请求失败回调
    @discardableResult
    func request(_ target: Target, param: [String: Any]? = nil, success: ((JSON) -> Void)? = nil, failure: FailureHandler) -> DataRequest {
        let req = NetWorkRequest.request(url: target.urlStr, params: param)
        requestsArr.append(req)
        return req
    }

}
